<?php

    $rootDir = $_SERVER['DOCUMENT_ROOT'];

    class SQLiteConnector {
    
        const DB_FILENAME = "blackjack.sqlite";
        
        private $_filename;
        private $_db;
        private $_connected;
        
        private static $_instance = NULL;
    
        private function __construct() {
            $this->_filename = self::DB_FILENAME;
            $this->_db = null;
            $this->_connected = false;
        }
        
        public function connect() {
            
            if (!$this->_connected) {
                $this->_db = new SQLiteDatabase($_SERVER['DOCUMENT_ROOT'].'/'.$this->_filename);
                $this->_connected = $this->_db ? true : false;
            }
        
            return $this->_connected;
            
        }
        
        public function getDB() {
            return $this->_db;
        }
        
        public function isConnected() {
            return $this->_connected;
        }
        
        public static function getInstance() {
            if (self::$_instance == NULL) {
                self::$_instance = new SQLiteConnector();
            }
            return self::$_instance;
        } 
        
        private function __clone() {
        }
        
        /* public function clearDB() {
            $this->_db->query();
        } */
        
    }
    
    /*
    $conn = new SQLiteConnector();
    echo "created class. ";
    if ($conn->connect()) {
        echo "connected. ";
        $db = $conn->getDB();        
        $q = $db->query('SELECT requests FROM tablename WHERE id = 1');
        //echo 'q is: '.$q;
        if ($q === false) {
            $db->queryExec('CREATE TABLE tablename (id int, requests int, PRIMARY KEY (id)); INSERT INTO tablename VALUES (1,1)');
            echo "created, then one hit more. ";
            $hits = 1;
        } else {
            $result = $q->fetchSingle();
            echo "result is: $result; fetched, then one hit more. ";
            $hits = $result+1;
        }
        $db->queryExec("UPDATE tablename SET requests = '$hits' WHERE id = 1");
        echo "updated, Hits: $hits. ";
    } else {
        die($err);
    } */

?>
